// OneWayFlightData实体类
package org.chen.common.flight;

import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

@Data
@TableName("flight_oneway_flight_data")
public class OneWayFlightData {
    private Long id;
    private String transactionId;
    private String searchRoute;
    private String itineraryId;
    private Integer priceIndex;
    private String flightNumber;
    private String airlineCode;
    private String airline;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime departureTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime arrivalTime;
    private String duration;
    private Integer crossDays;
    
    private String departureAirportCode;
    private String arrivalAirportCode;
    private String departureCityCode;
    private String arrivalCityCode;
    private String departureAirport;
    private String arrivalAirport;
    private String departureCity;
    private String arrivalCity;
    private String departureTerminal;
    private String arrivalTerminal;
    
    private BigDecimal adultPrice;
    private BigDecimal adultTax;
    private BigDecimal childPrice;
    private BigDecimal childTax;
    private BigDecimal infantPrice;
    private BigDecimal infantTax;
    private String currency;
    private Integer miseryIndex;
    private Integer ticketCount;
    
    private Integer transferCount;
    private Boolean isDirectFlight;
    private String cabinClass;
    private String baggageInfo;
    private String purchaseNotes;
    
    private String dataFingerprint;
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;

    /**
     * 从OneWayFlightInfo创建实体
     */
    public static OneWayFlightData fromOneWayFlightInfo(OneWayFlightInfo flight, String transactionId, String searchRoute) {
        OneWayFlightData data = new OneWayFlightData();
        
        data.setTransactionId(transactionId);
        data.setSearchRoute(searchRoute);
        data.setItineraryId(flight.getItineraryId());
        data.setPriceIndex(flight.getPriceIndex());
        data.setFlightNumber(flight.getFlightNumber());
        data.setAirlineCode(flight.getAirlineCode());
        data.setAirline(flight.getAirline());
        
        data.setDepartureTime(flight.getDepartureTime());
        data.setArrivalTime(flight.getArrivalTime());
        data.setDuration(flight.getDuration());
        data.setCrossDays(flight.getCrossDays());
        
        data.setDepartureAirportCode(flight.getDepartureAirportCode());
        data.setArrivalAirportCode(flight.getArrivalAirportCode());
        data.setDepartureCityCode(flight.getDepartureCityCode());
        data.setArrivalCityCode(flight.getArrivalCityCode());
        data.setDepartureAirport(flight.getDepartureAirport());
        data.setArrivalAirport(flight.getArrivalAirport());
        data.setDepartureCity(flight.getDepartureCity());
        data.setArrivalCity(flight.getArrivalCity());
        data.setDepartureTerminal(flight.getDepartureTerminal());
        data.setArrivalTerminal(flight.getArrivalTerminal());
        
        data.setAdultPrice(flight.getAdultPrice());
        data.setAdultTax(flight.getAdultTax());
        data.setChildPrice(flight.getChildPrice());
        data.setChildTax(flight.getChildTax());
        data.setInfantPrice(flight.getInfantPrice());
        data.setInfantTax(flight.getInfantTax());
        data.setCurrency(flight.getCurrency());
        data.setMiseryIndex(flight.getMiseryIndex());
        data.setTicketCount(flight.getTicketCount());
        
        data.setTransferCount(flight.getTransferCount());
        data.setIsDirectFlight(flight.getIsDirectFlight());
        data.setCabinClass(flight.getCabinClass());
        data.setBaggageInfo(flight.getBaggageInfo());
        
        // 处理购买须知
        if (flight.getPurchaseNotes() != null) {
            data.setPurchaseNotes(String.join("; ", flight.getPurchaseNotes()));
        }
        
        // 生成数据指纹用于去重
        data.setDataFingerprint(generateFingerprint(flight));
        data.setCreatedAt(LocalDateTime.now());
        data.setUpdatedAt(LocalDateTime.now());
        
        return data;
    }

    /**
     * 生成数据指纹
     */
    private static String generateFingerprint(OneWayFlightInfo flight) {
        StringBuilder sb = new StringBuilder();
        sb.append(flight.getFlightNumber()).append("|")
          .append(flight.getDepartureTime()).append("|")
          .append(flight.getArrivalTime()).append("|")
          .append(flight.getDepartureAirportCode()).append("|")
          .append(flight.getArrivalAirportCode()).append("|")
          .append(flight.getCabinClass()).append("|")
          .append(flight.getAdultPrice()).append("|")
          .append(flight.getAdultTax());
        
        return sb.toString().hashCode() + "";
    }
}